package com.example.liunanxin.lnx226ordering.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liunanxin.lnx226ordering.R;
import com.example.liunanxin.lnx226ordering.beans.LoginBean;
import com.example.liunanxin.lnx226ordering.iface.LoginListener;
import com.example.liunanxin.lnx226ordering.model.LoginModel;

public class LoginActivity  extends AppCompatActivity {
    TextView registerview;
    Button loginbtn,newregisterbtn;
    EditText edtname,edtpass;
    private String username,userpass;
    private  Switch sw;
    private SharedPreferences sharedPreferences;

    private LoginListener loginListener=new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getUserid().equals("0")){
                Toast.makeText(LoginActivity.this,"userid="+loginBean.getUserid(),Toast.LENGTH_SHORT).show();
            }
//            if(edtname.getText().toString()==null||edtpass.getText().toString()==null)
//            {
//                Toast.makeText(LoginActivity.this,"用户名和密码不为空",Toast.LENGTH_SHORT).show();
//
//            }
            else{
                Toast.makeText(LoginActivity.this,"userid="+loginBean.getUserid(),Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(LoginActivity.this,HomeActivity.class);
                sharedPreferences.edit()
                        .putString("userid",loginBean.getUserid())
                        .apply();
                startActivity(intent);
            }
        }

        @Override
        public void onFail(String mag) {
            Toast.makeText(LoginActivity.this,mag,Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        edtname=findViewById(R.id.editText1);
        edtpass=findViewById(R.id.editText2);
        loginbtn=findViewById(R.id.button5);
        newregisterbtn=findViewById(R.id.button4);
        registerview=findViewById(R.id.editText5);
        sw=findViewById(R.id.switch1);
        sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);

        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) edtpass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                //这个类相当于是一个工具类，专门用来转换输入字符的显示的。而setTransformationMethod 则可以支持将输入的字符转换，
                    // 如果需要实现自己的转换,可以通过实现 TransformationMethod 接口来达到你的目的（比如让输入的所有字符都变成 a，或者输入 a 显示 z，输入 z 显示 a 等）
                else edtpass.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (edtname.getText().toString().trim().equals("")||edtpass.getText().toString().trim().equals("")){
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                }
                else {
                    username=edtname.getText().toString();
                    userpass=edtpass.getText().toString();
                    LoginModel loginModel=new LoginModel();
                    loginModel.getLoginResult(username,userpass,loginListener);

                }
            }
        });

        newregisterbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}

